/**
 * 
 */
package Model;

import java.io.File;
import java.util.List;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Observable;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author enzo
 *
 */
public class ContactList {

	private List <contact> List;
	private Set test;
	
	public ContactList()
	{
		List=new ArrayList <contact>();
	}
	public void addContact(contact c)
	{
		List.add(c);
	}
	public void removeContact(contact c)
	{	//On retire le contact de la liste
		List.remove(c);
		// on supprime sa photo de la memoire
		if(c.getPhoto().contentEquals("Resources/"+c.getFirstname()+c.getName()+".png"))
		{
		File f = new File(c.getPhoto());
		f.delete();
		}
	}
	public void alphaSort()
	{
		Collections.sort(List, new AlphaComparator());
	}
	public void groupSort()
	{
		Collections.sort(List, new GroupComparator());
	}
	public void saveAgenda()
	{
		File fichier=null;
		fichier=new File("Resources/Agenda.csv");
		FileWriter fw;
		try {
			fw = new FileWriter(fichier);
			//fw.write("Name;Firstname;Address;Company Address; Phone perso; Phone pro; email perso; email pro; URL; picture; Group  \n");
			for(contact c : List)
			{
			fw.write(c.toCsv());
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void loadAgenda()
	{
		File fichier=new File("Resources/Agenda.csv");
		boolean firstLine = true;
		try {
			
			Scanner inputStream=new Scanner(fichier);
			while (inputStream.hasNext()) {
				// read single line, put in string
				String data = inputStream.nextLine();

				/*// if it's the first line, we skip it as it corresponds to titles
				if (firstLine == true ) {
					try{
					data = inputStream.nextLine();
					firstLine = false;
					}catch(NoSuchElementException e)
					{
						e.printStackTrace();
					}
				}*/

				// Split line into several data strings
				String dataTab[] = data.split(";");
				contact c = new contact(dataTab[0],dataTab[1],dataTab[2],dataTab[3],dataTab[4],dataTab[5],dataTab[6],dataTab[7],dataTab[8],dataTab[9],dataTab[10]);
				List.add(c);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @return the list
	 */
	public List<contact> getList() {
		return List;
	}
}
